package com.inspur.gs.sbssdl.sbssbxgl.sbssbx.sbssbx.back.commoncmps;

import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IKey;
import com.inspur.edp.dxc.spi.components.BaseCustomValidateComponent;
import com.inspur.edp.qdp.bql.api.IBqlExecuter;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.iec.edp.caf.runtime.config.BqlBeanUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

public class Gylbjtb extends BaseCustomValidateComponent {

    private static final Logger log = LoggerFactory.getLogger(Gylbjtb.class);
    private final IBqlExecuter bqlExecuter = BqlBeanUtil.getAppCtx().getBean(IBqlExecuter.class);

    //供应链同步表id
    private final String gylbj = "a38b0b52-72be-48e6-bf7c-f64a98dcac56";

    public void gylbjxxtb(){
        List<Gylbjtbxx> bjlist = mockData();

        SpringBeanUtils.getBean(IBefSessionManager.class).createSession();
        try {
            //删除后插入
            ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
            IStandardLcp gylbjlcp = iLcpFactory.createLcpByBEId(gylbj);
            List<IEntityData> oriList = gylbjlcp.query();
            List<String> ids = oriList.stream().map(s -> s.getID()).collect(Collectors.toList());
            gylbjlcp.delete(ids);

            bjlist.forEach(s->{
                IEntityData newData = gylbjlcp.retrieveDefault();
                ModifyChangeDetail change = new ModifyChangeDetail(newData.getID());
                change.getPropertyChanges().put("wzbh",s.getWzbh());
                change.getPropertyChanges().put("wzmc",s.getWzmc());
                change.getPropertyChanges().put("dj",s.getDj());
                change.getPropertyChanges().put("ggxh",s.getGgxh());
                change.getPropertyChanges().put("sccj",s.getSccj());
                gylbjlcp.modify(change);
            });

            gylbjlcp.save();
        } finally {
            SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
        }
    }


    public List<Gylbjtbxx> mockData() {
        List<Gylbjtbxx> list = new ArrayList<>();
        list.add(new Gylbjtbxx("GYL0001", "WZ100001", "高压电容器", new BigDecimal("5876.34"), "GX-A100", "西安西电"));
        list.add(new Gylbjtbxx("GYL0002", "WZ100002", "隔离开关", new BigDecimal("1234.56"), "GX-B200", "正泰集团"));
        list.add(new Gylbjtbxx("GYL0003", "WZ100003", "避雷器", new BigDecimal("888.88"), "GX-C300", "平高电气"));
        list.add(new Gylbjtbxx("GYL0004", "WZ100004", "电流互感器", new BigDecimal("3210.99"), "GX-D400", "南瑞继保"));
        list.add(new Gylbjtbxx("GYL0005", "WZ100005", "电压互感器", new BigDecimal("4567.89"), "GX-E500", "许继电气"));
        list.add(new Gylbjtbxx("GYL0006", "WZ100006", "断路器", new BigDecimal("7654.32"), "GX-A100", "特变电工"));
        list.add(new Gylbjtbxx("GYL0007", "WZ100007", "绝缘子", new BigDecimal("234.56"), "GX-B200", "山东电工"));
        list.add(new Gylbjtbxx("GYL0008", "WZ100008", "电缆接头", new BigDecimal("1987.65"), "GX-C300", "江苏大全"));
        list.add(new Gylbjtbxx("GYL0009", "WZ100009", "接地开关", new BigDecimal("678.90"), "GX-D400", "华仪电气"));
        list.add(new Gylbjtbxx("GYL0010", "WZ100010", "组合电器", new BigDecimal("9123.45"), "GX-E500", "天水二一三"));
        return list;
    }
}